package main.pmc;

import cn.hutool.core.util.ZipUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import lombok.SneakyThrows;
import utils.SmallTool;
import utils.StringUtil;
import utils.httpclient.HttpClient;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022/3/12 13:20
 */
public class DownPmcidHtml {
    // 用于下载pmcid的页面保存


    @SneakyThrows
    public static void main(String[] args) {
//        DSFactory.setCurrentDSFactory(new DruidDSFactory());
//        DataSource ds = DSFactory.get("taiyiserver");
        Db useTaiyi = Db.use();
        String saveTable = "pmcdownziphtml";
        String allDate = "select max(pmid) as pmid  from " + saveTable;
        Entity query = useTaiyi.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("pmid"));
        Integer count = query.getInt("pmid");
        int breakSize = 100000;
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("即将开始请提前关闭如果想的话");
        SmallTool.SleepMillis(5000L);
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("当前页码+++++" + i);
            SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);
            String paramSql = "select pmid,pmcid,html from " + saveTable + " where html is  null and  pmid between  " + start + " and " + end;
            List<Entity> querys = useTaiyi.query(paramSql);
            querys.forEach(entity -> {
                String pmcid = entity.getStr("pmcid");
                byte[] htmls = entity.getBytes("html");
                if (StringUtil.isNotNull(pmcid) && StringUtil.isNull(htmls)) {
                    String pmc = "https://pmc.ncbi.nlm.nih.gov/articles/PMC11724815";
                    String replace = pmc.replace("PMC11724815", pmcid);
                    try {
                        String htmlDirect = HttpClient.getHtmlDirect(replace);
                        if (htmlDirect != null) {
                            byte[] gzip = ZipUtil.gzip(htmlDirect, "utf-8");
                            entity.set("html", gzip);
                            useTaiyi.insertOrUpdate(entity, "pmid");
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                }

            });

        }


    }


}
